/**
 * 文件：GuideActivity.java
 * 包名：com.ronri.guide;   --- 正式发布时需要修正
 * 说明：引导页面
 * 作者：MS During
 * 日期：2017/5/14
 * 历史：
 */
package com.ronri.guide;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.common.BitmapManager;

import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;




public class GuideActivity
        extends Activity
        implements ViewPager.OnPageChangeListener {

    private ViewPager vp;
    private guideAdapter vpAdapter;
    private List<View> views;

    // 底部小点图片
    private ImageView[] dots;

    // 记录当前选中位置
    private int currentIndex;
    Boolean isFirst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.guide_layout);

        // 初始化页面
        initViews();

        // 初始化底部小点
        initDots();
    }

    private void initViews() {

        SharedPreferences pref = getSharedPreferences("first", Activity.MODE_PRIVATE);
        isFirst = pref.getBoolean("status",true);

        if(isFirst){
            finish();

            Intent intent = new Intent(this, com.ronri.ams.activity_login.class);
            startActivity(intent);

        }

        LayoutInflater inflater = LayoutInflater.from(this);
        RelativeLayout guideThree = (RelativeLayout) inflater.inflate(R.layout.guide_three, null);

        guideThree.findViewById(R.id.toMain).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

                Intent intent = new Intent(InitApplication.getContext(), com.ronri.ams.activity_login.class);
                startActivity(intent);
            }
        });

        views = new ArrayList<View>();


        // 初始化引导图片列表
        views.add(new myView(this, R.drawable.guide1));
        views.add(new myView(this, R.drawable.guide2));
        views.add(guideThree);

        // 初始化Adapter
        vpAdapter = new guideAdapter(views, this);

        vp = (ViewPager) findViewById(R.id.viewpager);
        vp.setAdapter(vpAdapter);
        // 绑定回调
        vp.setOnPageChangeListener(this);
    }

    private class myView extends View {
        private int mResourceId = -1;

        public myView(Context context, int ResouceId) {
            super(context);
            mResourceId = ResouceId;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            if (mResourceId != -1) {

                try {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    InputStream input = InitApplication.getContext().getResources().openRawResource(mResourceId);

                    SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
                    Bitmap bitmap = (Bitmap) softRef.get();

                    bitmap = Bitmap.createScaledBitmap(bitmap,
                            InitApplication.ScreenWidth,
                            InitApplication.ScreenHeight,
                            true);

                    canvas.drawBitmap(bitmap, 0, 0 , null);

                    softRef.clear();
                    // 马上释放
                    bitmap.recycle();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }
    }

    private void initDots() {
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

        dots = new ImageView[views.size()];

        // 循环取得小点图片
        for (int i = 0; i < views.size(); i++) {
            dots[i] = (ImageView) ll.getChildAt(i);
            dots[i].setEnabled(true);// 都设为灰色
        }

        currentIndex = 0;
        dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > views.size() - 1
                || currentIndex == position) {
            return;
        }

        dots[position].setEnabled(false);
        dots[currentIndex].setEnabled(true);

        currentIndex = position;
    }

    // 当滑动状态改变时调用
    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    // 当当前页面被滑动时调用
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    // 当新的页面被选中时调用
    @Override
    public void onPageSelected(int arg0) {
        // 设置底部小点选中状态
        setCurrentDot(arg0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        for(View view : views) {
            BitmapManager.recycleImageView(view);
            view.destroyDrawingCache();
        }
        vpAdapter = null;

        views.clear();

        views = null;

        // 清空后台
        setContentView(R.layout.s_free_layout);

        System.gc();
        System.runFinalization();
    }
}
